#!/bin/bash
#
# Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
# Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
# Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
#
# This software is available to you under a choice of one of two
# licenses.  You may choose to be licensed under the terms of the GNU
# General Public License (GPL) Version 2, available from the file
# COPYING in the main directory of this source tree, or the
# OpenIB.org BSD license below:
#
#     Redistribution and use in source and binary forms, with or
#     without modification, are permitted provided that the following
#     conditions are met:
#
#      - Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      - Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials
#        provided with the distribution.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# $Id: osm_indent 4707 2006-01-03 16:18:08Z halr $
#
#########################################################################
# 
#  Abstract:
#  	Indent script for source code formatting.
# 
#  Environment:
#  	Linux User Mode
# 
#  $Revision: 1.4 $
# 
#
# This is the indent format used for OpenSM.
#
# format the source code according to the ACD standard
# -bad	Blank line after declarations
# -bap	Blank line after Procedures
# -bbb	Blank line before block comments
# -nbbo	Break after Boolean operator
# -bl	Break after if line
# -bli0 Indent for braces is 0
# -bls	Break after struct declarations
# -cbi0	Case break indent 0
# -ci3	Continue indent 3 spaces
# -cli0	Case label indent 0 spaces
# -ncs	No space after cast operator
# -hnl	Honor existing newlines on long lines
# -i3	Substitute indent with 3 spaces
# -npcs	No space after procedure calls
# -prs	Space after parenthesis
# -nsai	No space after if keyword - removed
# -nsaw	No space after while keyword - removed
# -sc	Put * at left of comments in a block comment style
# -nsob	Don't swallow unnecessary blank lines
# -ts3	Tab size is 3
# -psl	Type of procedure return in a separate line
# -bfda	Function declaration arguments in a separate line.
# -nut   No tabs as we allow spaces
#
#########################################################################

# indent the world
for sourcefile in $*; do
    if test -f "$sourcefile"; then
        # first, string DOS style linefeeds
        perl -piW -e's/\x0D//' "$sourcefile"
        echo Processing $sourcefile
        indent -bad -bap -bbb -nbbo -bl -bli0 -bls -cbi0 -ci3 -cli0 -ncs \
                -hnl -i3 -npcs -prs -sc -nsob -ts3 -psl -bfda -nut $sourcefile

        rm ${sourcefile}W

        # the -bb also affect the first line in each file - so clean it up
        if test `head -1 $sourcefile | egrep -v '^$' | wc -l` = 0; then
            echo Cleaning up first empty line of $sourcefile
            awk '{if(n){print};n++}' $sourcefile > ${sourcefile}W
            mv -f ${sourcefile}W $sourcefile
        fi
    else
        echo Could not find file:$sourcefile
    fi
done
